import tarfile
import gzip
import os
import sys

# Data preparation related
data_url = "www.openslr.org/resources/33"
remove_archive = False
download_opt = None


def untar(file_path, untar_dir):
    try:
        tar_obj = tarfile.open(file_path)
        # names = tar_obj.getnames()
        # print(names)
        tar_obj.extractall(path=untar_dir)
        return True
    except Exception as e:
        print(e)
        return False


def ungzip(gzip_file):
    gzip_replace = gzip_file.replace(".gz", "")
    gzip_obj = gzip.GzipFile(gzip_file)
    open(gzip_replace, "wb+").write(gzip_obj.read())
    gzip_obj.close()


def ungzip_subdir():
    dir_name = "/public/solution/2022_cmcc_model/conformer/dataset/data_aishell/wav"
    dst_dir = "/public/ai_platform/models/cmcc_2022/conformer_dataset/data_aishell"

    for root, dirs, files in os.walk(dir_name):
        for file in files:
            if file.endswith("tar.gz"):
                file_path = os.path.join(root, file)
                print(file_path)
                ungzip(file_path)
                untar(file_path.replace(".gz", ""), dst_dir)
                os.remove(file_path.replace(".gz", ""))


if __name__ == '__main__':
    tar_file = ""
    untar_dir = ""
    # untar(tar_file, untar_dir)
    ungzip_subdir()
